public class HappyNum {
    //快乐数:https://leetcode.cn/problems/happy-number/description/
    public boolean isHappy(int n) {
        int slow=n,fast=bitSum(n);
        while(slow!=fast) {
            slow=bitSum(slow);
            fast=bitSum(bitSum(fast));
        }
        return slow==1;
    }
    //返回数n的每位数字的平方和
    private int bitSum(int n) {
        int sum=0;
        while(n!=0) {
            int t=n%10;
            sum+=t*t;
            n/=10;
        }
        return sum;
    }
}
